/**
 * 关联产品分类
 * @author linfeng
 * @since 2019/08/19
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    form.render(null, "zmn-categ-form");

    let ajaxIndex;
    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let url = "/serv/categ/ass/batchAddAssociation.action";
        let showPlatArr = new Array();
        let showPlat = document.getElementsByName("showPlat");
        let str = new Array();
        for(let k in showPlat){
            if(showPlat[k].checked){
                var servAssCategory = new Object();
                servAssCategory.servCategId = json.servCategId;
                str = showPlat[k].value.split(",");
                servAssCategory.categOneId = parseInt(str[0]);
                servAssCategory.categId = parseInt(str[1]);
                servAssCategory.showType = 1;
                showPlatArr.push(servAssCategory);
            }
        }

        if(showPlatArr.length == 0){
            layer.msg("关联分类不能为空");
            return false;
        }

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json",
            data: JSON.stringify({servAssCategoryList:showPlatArr, showType:1, servCategoryId:json.servCategId}),
            beforeSend: function () {
                ajaxIndex = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success:function(json) {
                layer.msg(json.message);
                if(json.status == 200) {
                    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                } else {
                    layer.msg(json.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){}
        })

        layer.close(ajaxIndex);
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        parent.layui.element.tabDelete('layadmin-layout-tabs', window.location.pathname + window.location.search);
    });

    //对外暴露的接口
    exports('art_serv_categ_classify', {});
});